﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ScaffoldIt.Configuration;

namespace ScaffoldIt
{
    public partial class FormParameters : Form
    {
        public FormParameters()
        {
            InitializeComponent();
        }

        public static List<Parameter> GetParameters(List<Parameter> parameters)
        {
            using (var frm = new FormParameters())
            {
                frm.InitForm(parameters);
                if (frm.ShowDialog() == DialogResult.OK)
                {
                    frm.GetValues();
                    return parameters;
                }
            }

            return null;
        }

        private void InitForm(List<Parameter> parameters)
        {
            for (int i = parameters.Count - 1; i >= 0; i-- )
            {
                InitParameter(parameters[i]);
            }
        }

        private void InitParameter(Parameter parameter)
        {
            Label lblSpacer = new Label() { Text = "   " };
            lblSpacer.Dock = DockStyle.Top;
            lblSpacer.Font = fontDesc;
            lblSpacer.Height = fontDesc.Height;
            this.panelContent.Controls.Add(lblSpacer);

            if (parameter.Type == Parameter.PARAM_CHOICE)
            {                
                ComboBox cbx = new ComboBox();
                cbx.Tag = parameter;
                cbx.DropDownStyle = ComboBoxStyle.DropDownList;
                cbx.Dock = DockStyle.Top;
                cbx.Items.AddRange(parameter.Choices.Split(',').Select(s => s.Trim()).ToArray());

                panelContent.Controls.Add(cbx);
                cbx.SelectedItem = parameter.Value;
                GeneratePreview(parameter);
            }
            else if (parameter.Type == Parameter.PARAM_BOOL)
            {
                GenerateDesc(parameter);

                CheckBox txt = new CheckBox() { Text = parameter.Title + " (" + parameter.Code + ")" };
                txt.Tag = parameter;
                txt.Dock = DockStyle.Top;
                txt.Font = fontTitle;
                //txt.Height = fontTitle.Height;
                if (string.Compare(parameter.Value, "true", StringComparison.OrdinalIgnoreCase) == 0) 
                    txt.Checked = true;
                panelContent.Controls.Add(txt);
            }
            else
            {                
                TextBox txt = new TextBox();
                txt.Tag = parameter;
                txt.Dock = DockStyle.Top;
                txt.Text = parameter.Value;

                panelContent.Controls.Add(txt);

                GeneratePreview(parameter);
            }
        }

        private void GeneratePreview(Parameter parameter)
        {
            GenerateDesc(parameter); 
            
            GenerateTitle(parameter);            
        }

        private static Font fontDesc = new Font("Arial", 8, FontStyle.Italic);
        private static Font fontTitle = new Font("Arial", 9, FontStyle.Bold);

        private void GenerateDesc(Parameter parameter)
        {
            if (!string.IsNullOrEmpty(parameter.Description))
            {
                Label lblDesc = new Label() { Text = parameter.Description };
                lblDesc.Dock = DockStyle.Top;
                lblDesc.Font = fontDesc;
                lblDesc.Height = fontDesc.Height + 2;
                this.panelContent.Controls.Add(lblDesc);
            }
        }

        private void GenerateTitle(Parameter parameter)
        {
            Label lblTitle = new Label() { Text = parameter.Title + " (" + parameter.Code + ")" };
            lblTitle.Dock = DockStyle.Top;
            lblTitle.Font = fontTitle;
            lblTitle.Height = fontTitle.Height + 2;
            this.panelContent.Controls.Add(lblTitle);
        }

        private void GetValues()
        {
            foreach (var control in panelContent.Controls)
            {
                if (control is TextBox)
                {
                    var parameter = (Parameter)((TextBox)control).Tag;
                    parameter.Value = ((TextBox)control).Text;
                }
                else if (control is CheckBox)
                {
                    var parameter = (Parameter)((CheckBox)control).Tag;
                    parameter.Value = ((CheckBox)control).Checked.ToString();
                }
                else if (control is ComboBox)
                {
                    var parameter = (Parameter)((ComboBox)control).Tag;
                    parameter.Value = (string)((ComboBox)control).SelectedItem;
                }
            }
        }

        
    }
}
